package com.llsydn.graphics;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.llsydn.hello.R;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author lilinshen
 * @title Bitmap图片测试
 * @description 请填写相关描述
 * @date 2018/10/11 9:23
 */
public class BitmapTestActivity extends AppCompatActivity {

    private ImageView iv_bitmap1;
    private ImageView iv_bitmap2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_test);

        iv_bitmap1 = (ImageView) findViewById(R.id.iv_bitmap1);
        iv_bitmap2 = (ImageView) findViewById(R.id.iv_bitmap2);

        //需求1: 加载资源文件中的图片资源并显示
        iv_bitmap1.setImageResource(R.drawable.ic_launcher);

        //需求2: 加载存储空间中的图片资源并显示
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/f1.jpg");
        iv_bitmap2.setImageBitmap(bitmap);
    }

    public void saveImage(View v) throws FileNotFoundException {
        // 需求3: 将一个bitmap对象保存到存储空间中
        Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/f1.jpg");
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, openFileOutput("f1.jpg", Context.MODE_PRIVATE));
        Toast.makeText(this, "保存完成", Toast.LENGTH_SHORT).show();
    }
}
